package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;


    @RequestMapping("/list")
    @RequirePermission(name = "部门查询",expression = "department:list")
    public String list(Model model, QueryObject qo) {
        model.addAttribute("pageInfo", departmentService.query(qo));
        return "department/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "部门删除",expression = "department:delete")
    public String delete(Long id) {
        if (id != null) {
            departmentService.delete(id);
        }
        return "redirect:/department/list";
    }

    /*@RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "部门编辑",expression = "department:saveOrUpdate")
    public String saveOrUpdate(Department department) {
        if (department.getId() != null) {
            departmentService.update(department);
        } else {
            departmentService.insert(department);
        }
        return "redirect:/department/list";
    }*/

    // 异步提交表单方式
    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "部门编辑", expression = "department:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        if (department.getId() != null) {
            departmentService.update(department);
        } else {
            departmentService.insert(department);
        }
        return new JsonResult(true,"添加成功");
    }
}
